package fr.greta.centre.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.greta.centre.dao.CandidatDAOImpl;
import fr.greta.centre.dao.CandidatureDAOImpl;
import fr.greta.centre.dao.PromotionDAOImpl;
import fr.greta.centre.metier.Candidat;
import fr.greta.centre.metier.Candidature;
import fr.greta.centre.metier.Promotion;

/**
 * Servlet implementation class AfficheAdresseStagiare
 */
public class AfficheAdresseStagiare extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AfficheAdresseStagiare() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String paramIdPromo = request.getParameter("idPromotion");
        if (paramIdPromo == null) {
            request.getRequestDispatcher("googlerecherche.jsp").forward(
                    request, response);
        } else {
            if (paramIdPromo.matches("^ *$")) {
                request.setAttribute("message", "idPromotion est requis");
                System.out.println(request.getAttribute("message"));
                request.getRequestDispatcher("googlerecherche.jsp").forward(request,
                        response);
            } else {
                try {
                    int idPromo = Integer.parseInt(paramIdPromo);
                    // CandidatureDAOImpl dao = new CandidatureDAOImpl();
                    // List<Candidature> candidatures =
                    // dao.findByIdPromotion(idPromo);
                    // request.setAttribute("candidatures", candidatures);
                    CandidatDAOImpl dao = new CandidatDAOImpl();
                    List<Candidat> lesCandidats = dao.findAllAdress(idPromo);
                    if (!lesCandidats.isEmpty()) {
                        request.setAttribute("candidats", lesCandidats);
                        request.getRequestDispatcher("googleaffiche.jsp")
                                .forward(request, response);
                    } else {
                        request.setAttribute("message",
                                "Aucun candidat n'a été trouvé pour cette promotion");
                        request.getRequestDispatcher("message.jsp").forward(
                                request, response);
                    }
                } catch (NumberFormatException exc) {
                    request.setAttribute("message", "idPromotion est requis");
                    request.getRequestDispatcher("message.jsp").forward(
                            request, response);
                } catch (SQLException exc) {
                    request.setAttribute("message", exc.getMessage());
                    request.getRequestDispatcher("message.jsp").forward(
                            request, response);
                }
            }
        }
    }
}

